const wx2my = require('../../wx2my');

var interval = null; //倒计时函数

const app = getApp();
Page({
  data: {
    codename: '获取验证码',
    //倒计时
    currentTime: 60
  },

  onLoad(e) {
    this.setData({
      phone: e.phone
    });
  },

  get_phone() {
    let that = this;
  },

  inputAccount(e) {
    let that = this;
    console.log(e);
    app.wxRequest('get_mobile', {
      'adminname': e.detail.value
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          phone: res.data.data.mobile
        });
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  formSubmit(e) {
    console.log(e);

    if (this.data.onOff == 1) {
      return false;
    }

    ;
    let that = this;
    let res = e.detail.value;
    console.log(res);

    if (app.fm.isNull(res.passwords)) {
      app.toast('请输入您的新密码');
    } else {
      that.setData({
        onOff: 1
      });
      app.wxRequest('forgotten_password', {
        'adminname': res.adminname,
        'new_password': res.passwords,
        'sms': res.captcha,
        'account': res.phone
      }, function (res) {
        console.log(res);

        if (res.data.code == 1) {
          setTimeout(function () {
            wx2my.navigateBack();
          }, 2000);
        } else {
          that.setData({
            onOff: 0
          });
        }

        app.toast(res.data.msg);
      });
    }
  },

  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime;
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        codename: currentTime + '秒'
      });

      if (currentTime <= 0) {
        clearInterval(interval);
        that.setData({
          codename: '重新发送',
          currentTime: 60,
          disabled: false
        });
      }
    }, 1000);
  },

  getVerificationCode() {
    var that = this;
    app.wxRequest('sms', {
      'account': that.data.phone
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.getCode();
      } else {
        app.toast(res.data.msg);
      }
    });
    that.setData({
      disabled: true
    });
  }

});